<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><b>用例管理</b></el-breadcrumb-item>
            <el-breadcrumb-item>用例列表</el-breadcrumb-item>
            <el-card class="box-card" shadow="never">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <div class="grid-content bg-purple">
                            <el-button class="el-insert" type="primary" @click="dialogVisible = true">新增用例</el-button>
                        </div>
                    </el-col>
                </el-row>
                <el-table  stripe  v-loading="loading" :cell-style="rowStyle" border style="width:100%" :data="caseList" class="el-table">
                    <el-table-column align="center" prop="case_id" label="用例编号" width="180"></el-table-column>
                    <el-table-column align="center" prop="case_description" label="用例描述" width="180"></el-table-column>
                    <el-table-column align="center" prop="case_state" label="用例状态" width="180"></el-table-column>
                    <el-table-column align="center" prop="case_createTime" label="用例创建时间" width="180"></el-table-column>
                    <el-table-column align="center" prop="" label="操作">
                        <template v-slot="scope">
                            <!-- 作用域插槽 -->
                            <!-- 删除按钮 -->
                            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                                <el-button type="danger" icon="el-icon-delete" size="mini"
                                    @click="deleteCase(scope.row.case_id)">删除</el-button>
                            </el-tooltip>
                            <!-- 分配角色按钮 -->
                            <el-tooltip effect="dark" content="发布任务" placement="top" :enterable="false">
                                <el-button type="success" icon="el-icon-delete" size="mini"
                                    @click="showPublishTask(scope.row.case_id)">发布任务</el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页组件 -->
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum" :page-sizes="[1, 3, 5, 6]" :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total=this.total>
                </el-pagination>
            </el-card>
            <el-dialog v-dialog-drag title="新增用例" :visible.sync="dialogVisible" width="30%"
                @close="dialogClose('addFormRef')">
                <el-form label-width="100px" :model="addForm" ref="addFormRef" :rules="rules">
                    <el-form-item label="用例描述" prop="case_description">
                        <el-input v-model="addForm.case_description"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addCase('addFormRef')">添加</el-button>
                </div>
            </el-dialog>

            <el-dialog v-dialog-drag title="发布任务" :visible.sync="publishdialogVisible" width="30%">
                <el-form label-width="100px" :model="caseinfo" :rules="rules">
                    <el-form-item label="用例编号">
                        <el-input v-model="caseinfo.case_id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="测试人">
                        <el-select v-model="options.value" placeholder="请选择测试人账号"  multiple>
                            <el-option v-for="item in options" :key="item.tester_id" :label="item.tester_username"
                                :value="item.tester_id" :disabled="item.disabled">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="publishdialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="publishTask">保存</el-button>
                </div>
            </el-dialog>
        </el-breadcrumb>
    </div>
</template>
<script>

export default {
    data() {
        return {
            arr: [],
            disabled: false,
            //下拉菜单数据
            value: "",
            options: [
            ],
            //用例列表
            caseList: [],
            //任务列表
            taskList: [],
            //发布任务对话框的信息
            pushlishForm: [],
            //关键字参数
            tester_id: "",
            queryInfo: {
                //当前页数
                pagenum: "1",
                //每页多少条
                pagesize: "4"
            },
            //测试员数量
            total: "",
            //对应id的用户信息
            caseinfo: [],
            value: true,
            currentPage1: 5,
            //添加用户对话框是否显示
            dialogVisible: false,
            //修改用户信息对话框是否显示
            publishdialogVisible: false,
            //添加用户
            addForm: {
                tester_id: "",
                tester_name: "",
                tester_experience: "",
                tester_phone: "",
                tester_username: "",
                tester_password: ""
            },
            //修改用户
            updateForm: {},
            rules: {
                tester_id:
                    [
                        { required: true, trigger: "blur" }
                    ],
                tester_name: [
                    { required: true, message: "请输入用户名", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在2到8位之间", trigger: "blur" },
                ],
                tester_experience: [
                    { required: true, message: "请输入工作经验", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在2位以上", trigger: "blur" },
                ],
                tester_phone: [
                    { required: true, message: "请输入电话", trigger: "blur" },
                    { min: 7, max: 11, message: "长度在7到11位之间", trigger: "blur" },
                ],
                tester_username: [
                    { required: true, message: "请输入账户名", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在2到8位之间", trigger: "blur" },
                ],
                tester_password: [
                    { required: true, message: "请输入密码", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在2到8位之间", trigger: "blur" },
                ],
            },
        };
    },
    created() {
        // this.getOptions()
        this.getCaseList()
        this.handleSizeChange(pagesize)
        this.handleCurrentChange(pagenum)
    },
    methods: {
        rowStyle() {
           return "text-align:center";
        },

        //获取用例列表&&分页列表
        async getCaseList() {
            const { data: res } = await this.$http.get("/api/case/page", {
                params: this.queryInfo
            })
            if (res.status != 200) {
                console.log("no data")
            }
            else {
                this.caseList = res.data
                this.total = res.total
            }
        },
        //监听pagesize的事件
        handleSizeChange(pagesize) {
            this.queryInfo.pagesize = pagesize;
            this.getCaseList();
        },
        //监听页码值改变事件
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getCaseList();
        },
        //添加新用户
        addCase(addFormRef) {
            this.$refs[addFormRef].validate(async (valid) => {
                if (!valid) return;
                const { data: res } = await this.$http.post("/api/caseinfo/insert", this.addForm);
                if (res.meta.status !== 200) {
                    return this.$message.error("用例添加失败");
                }
                this.dialogVisible = false;
                this.getCaseList();
                return this.$message.success("用例添加成功");
            });
        },
        //展示修改用户对话框并展示信息
        async showUpdateDialog(id) {
            this.updatedialogVisible = true
            const { data: res } = await this.$http.get("/api/user", {
                params: {
                    "tester_id": id
                }
            });
            if (res.meta.status != 200) {
                return this.$message.error("获取列表失败");
            }
            else {
                this.updateForm = res.data
            }
        },
        //修改用户信息
        async updateUser() {
            const { data: res } = await this.$http.post("/api/user/update", this.updateForm)
            if (res.meta.status != 200) {
                this.$message.error("修改失败")
            }
            else {
                this.$message.success("修改成功")
                this.getCaseList()
            }
            this.updatedialogVisible = false
        },
        //删除用户信息
        async deleteUser(id) {
            const { data: res } = await this.$http.get("/api/user/delete", {
                params: {
                    "tester_id": id
                }
            })
            if (res.meta.status != 200) {
                return this.$message.error("删除用户失败");
            }
            else {
                this.$message.success("删除用户成功")
                this.getCaseList();
            }
        },
        //获取下拉菜单数据
        async getOptions(id) {
            var _options
            const { data: res } = await this.$http("/api/user/findAll")
            if (res.meta.status != 200) {
                this.$message.error("查找失败")
            }
            else {
                _options = res.data
            }
            const { data: res1 } = await this.$http.get("/api/testerinfo_caseinfo", {
                params: {
                    "case_id": id
                }
            })
            if (res1.meta.status != 200) {
                console.log("no data")
            }
            else {
                this.taskList = res1.data
            }
            for(let i=0;i<_options.length;i++){
                for(let j=0;j<this.taskList.length;j++){
                    if(_options[i].tester_id===this.taskList[j].tester_id){
                       _options[i].disabled=true
                    }
                }
             }
              this.options =_options
        },
        //展示发布任务对话框
        async showPublishTask(id) {
            this.getOptions(id)
            const { data: res } = await this.$http("/api/case", {
                params: {
                    "case_id": id
                }
            })
            if (res.meta.status != 200) {
                this.$message.error("查找失败")
            }
            else {
                this.caseinfo = res.data
            }
            this.publishdialogVisible = true
        },
        //发布任务
        async publishTask() {
            const obj = {
                "case_id": this.caseinfo.case_id,
                "idlist": this.options.value
            }
            const { data: res } = await this.$http.post('/api/case/publish', obj)
            if (res.meta.status != 200) {
                this.$message.error("发布失败")
            }
            else {
                this.$message.success("发布成功")
            }
            this.publishdialogVisible=false
        },
        async deleteCase(id){
            const {data:res}= await this.$http.get("/api/caseinfo/delete",{
                params:{
                    "case_id":id
                }
            })
            if(res.meta.status!==200)return
            this.$message.success("删除成功")
            this.getCaseList()
             
        }
    }
}
</script>
<style scoped>
.box-card {
    margin-top:150px;
    width:100%;
}
.el-table {
    margin-top:20px;

}
</style>